<template>
	<div class="newDish">
		<!-- dish-nav -->
		<div class="navBg">
			<a href="#" class="me-4">新碟上架</a>
			<span class="more">
				<a href="#">更多</a>
				<i>&nbsp;</i>
			</span>
		</div>
		<!-- dish-roll -->
		<div class="n-dish">
			<div class="inner">
				<a href="javascript:void(0)" class="pre arrow-bg" @click="throttle(pre)"></a>
				<div class="rollDish">
					<div class="dishUl" :style="{left:`-${leftVal}px`,transition:`${ition}s`}">
						<div class="dishItem d-flex" v-for="(item,index) in newDishData.imgList">
							<div class="listItem" v-for="(items,indexs) in item">
								<div class="dishCover">
									<img :src="items.cover" class="img-fluid">
									<a href="#" class="msk" :title="items.title"></a>
									<a href="#" class="icon-play" title="播放"></a>
								</div>
								<p href="#" class="dishTitle dishWord" :title="items.title">{{items.title}}</p>
								<p href="#" class="dishDes dishWord" :title="items.des">{{items.des}}</p>
							</div>
						</div>
						<!-- 将第一组复制一次，实现无缝隙轮播-->
						<div class="dishItem d-flex">
							<div class="listItem" v-for="(item,index) in newDishData.imgList[0]">
								<div class="dishCover">
									<img :src="item.cover" class="img-fluid">
									<a href="#" class="msk" :title="item.title"></a>
									<a href="#" class="icon-play" title="播放"></a>
								</div>
								<p href="#" class="dishTitle dishWord" :title="item.title">{{item.title}}</p>
								<p href="#" class="dishDes dishWord" :title="item.des">{{item.des}}</p>
							</div>
						</div>
					</div>
				</div>
				<a href="javascript:void(0)" class="nxt arrow-bg" @click="throttle(nxt)"></a>
			</div>
		</div>
	</div>
</template>

<script setup>
	import {reactive,ref} from 'vue'
	import axios from 'axios'
	
	const newDishData=reactive({
		dishList:[],
		imgList:[],
	})
	
	const flag=ref(true);//防止重复点击
	const leftVal=ref(0);//盒子偏移量
	const ition=ref(0.8);//轮播过渡时间
	// const imgShow=ref(0);//当前显示图片列表索引
	const imgWidth=ref(645);//图片宽度
	// let imgList;
	axios.get(`https://mock.apifox.cn/m1/2897896-0-default/newDish`)
	.then(function (response) {
		newDishData.dishList=response.data.newDish;
		// 对获得数据进行分割
		let ImgArr=function(){
			let arr=[];
			let num=5 //5个一组
			if(newDishData.dishList.length){
				// 通过循环将dishList中数据分为两组
				for (let i=0;i<newDishData.dishList.length;i+=num){
					arr.push(newDishData.dishList.slice(i,i+num));
				}
			}
			return arr;
		}
		newDishData.imgList=ImgArr();
		// console.log(newDishData.dishList)
		// console.log(newDishData.imgList);
	})
	// 防止重复点击
	const throttle=(fun)=>{
		if(flag.value){
			flag.value=false;
			fun();//调用pre或nxt
			setTimeout(()=>{
				flag.value=true;
			},1200);//节流间隔时间，1200ms后才能再次点击
		}
	}
	const pre=()=>{
		// console.log('pre')
		if(leftVal.value==0){ //如果是第一组图片列表
			ition.value=0 //瞬间移到最后一组图片列表
			leftVal.value=(newDishData.imgList.length)*imgWidth.value// 瞬间移动
			setTimeout(()=>{ //通过延时障眼法,归原过渡时间,执行真正的“上一张”函数
				ition.value=0.8
				leftVal.value -=imgWidth.value
			},ition.value*1000)
		}
		else{
			ition.value=0.8
			leftVal.value -= imgWidth.value
		}
	}
	const nxt=()=>{
		if(leftVal.value==(newDishData.imgList.length-1)*imgWidth.value){//如果是最后一组图片
			ition.value=0.8
			leftVal.value+=imgWidth.value
			setTimeout(()=>{
				ition.value=0
				leftVal.value=0 //回到第一组图片
			},ition.value*1000)
		}else{
			ition.value=0.8
			leftVal.value+=imgWidth.value
		}
		// $('.dishUl').css('left','-645px');
		// const left=$('.dishUl').css('left')
		// if(left=='-645px'){
		// 	$('.dishUl').css('left','0');
		// }
		// if(left=='0px'){
		// 	$('.dishUl').css('left','-645px');
		// }
	}
</script>

<style scoped>
	a{
		text-decoration: none;
	}
	p{
		padding: 0;
		margin: 0;
	}
	/* nav */
	.newDish{
		margin-top: 35px;
	}
	.navBg,.more i,.arrow-bg,.listItem,.arrow-bg{
		background: url('../assets/hotImg/index.png') no-repeat 0 9999px;
	}
	.listItem .msk{
		background: url('../assets/new/coverall.png');
	}
	.icon-play{
		background: url('../assets/hotImg/iconall.png') no-repeat;
	}
	.navBg{
		height: 33px;
		padding: 0 10px 0 34px;
		position: relative;
		border-bottom: 2px solid #C10D0C;
		background-position: -225px -156px;
	}
	.navBg a{
		font-size: 20px;
		color: #333;
	}
	.more a{
		color: #666;
		font-size: 12px
	}
	.more{
		position: absolute;
		right: 12px;
	}
	.more i{
		display: inline-block;
		width: 12px;
		height: 12px;
		margin-left: 4px;
		vertical-align: middle;
		background-position: 0 -240px;
	}
	/* dish-roll */
	.n-dish{
		position: relative;
		zoom: 1;
		height: 186px;
		margin: 20px 0 37px;
		border: 1px solid #d3d3d3;
	}
	.inner{
		height: 184px;
		padding-left: 16px;
		background: #f5f5f5;
		border: 1px solid #fff;
	}
	.rollDish{
		width: 645px;
		height: 180px;
		overflow: hidden;
		position: relative;
	}
	.dishUl{
		position: absolute;
		top: 0;
		width: 645px;
		margin: 28px 0 0 0;
		display: flex;
	}
	.listItem{
		width: 118px;
		height: 150px;
		margin-left: 11px;
		background-position: -260px 100px;
	}
	.listItem .msk{
		position: absolute;
		top: 0;
		left: 0;
		width: 118px;
		height: 100px;
		background-position: 0 -570px;
	}
	.dishCover{
		width: 100px;
		height: 100px;
		margin-bottom: 7px;
		position: relative;
	}
	.dishCover:hover .icon-play{
		display: block;
	}
	.icon-play{
		display: none;
		width: 22px;
		height: 22px;
		position: absolute;
		left: 72px;
		bottom: 5px;
		background-position: 0 -85px;
	}
	.icon-play:hover{
		background-position: 0 -110px;
	}
	.dishTitle{
		color: #000;
	}
	.dishDes{
		color: #666;
	}
	.dishWord{
		font-size: 12px;
		width: 88%;
		line-height: 18px;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		cursor: pointer;
	}
	.dishWord:hover,.more a:hover{
		text-decoration: underline;
	}
	/* pre nxt */
	.pre{
		background-position: -260px -75px;
		left: 4px;
	}
	.arrow-bg{
		position: absolute;
		top: 71px;
		width: 17px;
		height: 17px;
	}
	.nxt{
		right: 4px;
		background-position: -300px -75px;
	}
</style>